package com.example.AIstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.AIstudy.dao.QuestionDao;
import com.example.AIstudy.domain.Question;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, Question> implements QuestionService {

    @Autowired
    QuestionDao questionDao;

    @Override
    public List<Integer> getLevelByStuID(int stuID) {
        return questionDao.getLevelByStuID(stuID);
    }

    @Override
    public void insertQuestion(Question question) throws AIstudyException {
        try{
            questionDao.insertQuestion(question);
        }catch (Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.INSERT_ERROR,"添加问题信息失败");
        }
    }

    @Override
    public IPage<Question> findQuestion(Map<String, Object> condition,Integer pageNum, Integer pageSize) {
        IPage page = new Page(pageNum, pageSize);
        QueryWrapper<Question> qw = new QueryWrapper<>();
        qw.like(condition.containsKey("question"), "question", condition.get("question"));
        //学生查询问题信息的时候可以筛选
        qw.like(condition.containsKey("queslevel"), "queslevel", condition.get("queslevel"));
        qw.ge(condition.containsKey("minTime"), "questime", condition.get("minTime"));
        qw.le(condition.containsKey("maxTime"), "questime", condition.get("maxTime"));
        return questionDao.findQuestion(page, qw);
    }
}
